package webservice;

import java.net.MalformedURLException;
import java.net.URL;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import beans.interfaces.ChatDataLocal;
import beans.interfaces.UserSyncLocal;
import model.Host;
import model.LoggedInUsers;
import model.User;
import exceptions.UsernameExistsException;

//Potrebno izvuci potrebne metode i prebaciti u ChatData


@WebService(endpointInterface = "webservice.ChatServiceWS", serviceName = "ChatService", targetNamespace = "http://1337.webservice")
@Stateless
@Remote(ChatServiceWS.class)
public class ChatServiceBean implements ChatServiceWS {

	@EJB
	ChatDataLocal chatData;
	
	@EJB
	UserSyncLocal userSync;
	
	NodeCommunicationWS service;
	
	@Override
	public User register(String username, String password)
			throws UsernameExistsException {
			User registered = chatData.register(username, password);
			login(username, password);
			return registered;
	}

	@Override
	public User login(String username, String password) {
		User login = chatData.getUser(username);
		if(login == null)
			return null;
		
		if(chatData.login(login) == false)
			return null;
		
		userSync.syncWithClient(login);
		
		String httpHeader = chatData.getHttpHeader();
		String wsdlLocation = chatData.getWsdlLocation();
		QName serviceName = chatData.getServiceName();
		
		try {
			//ArrayList<User> newUserList = chatData.getAllUsers();
			
			//obavestavanje svih hostova da doda user-a
			for(Host h : chatData.getHosts()) {
				if(!h.equals(chatData.getLocalHost())) {
					URL url = new URL(httpHeader+h.getAddress()+wsdlLocation);
					Service tempService = Service.create(url,serviceName);
					service = tempService.getPort(NodeCommunicationWS.class);
					//service.updateUserList(newUserList);
					//service.updateUserList(login);
				}
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		return login;
	}
	
	@Override
	public boolean logout(User user) {
		boolean retVal = chatData.logout(user);
		userSync.syncWithClient(user);
		
		String httpHeader = chatData.getHttpHeader();
		String wsdlLocation = chatData.getWsdlLocation();
		QName serviceName = chatData.getServiceName();
		
		try {
			
			//obavestavanje svih hostova da izbaci user-a
			for(Host h : chatData.getHosts()) {
				if(!h.equals(chatData.getLocalHost())) {
					URL url = new URL(httpHeader+h.getAddress()+wsdlLocation);
					Service tempService = Service.create(url,serviceName);
					service = tempService.getPort(NodeCommunicationWS.class);
					//service.updateUserList(user);
				}
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		return retVal;
	}

	@Override
	public LoggedInUsers getAllUsers() {
		LoggedInUsers liu= new LoggedInUsers();
		liu.setLoggedUsers(chatData.getAllUsers());
		return liu;
	}
}
